using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//<summary>
//负责处理玩家飞船的所有行为，包括移动、射击和边界限制。
//</summary>
public class Player : MonoBehaviour
{
    [Header("移动与物理参数")]
    [Tooltip("玩家飞船移动速度")]
    public float speed = 10f;
    
    [Header("射击相关")]
    public GameObject bulletPrefab; //获取子弹预制体
    public Transform firePoint;     //获取子弹发射位置
    
    [Header("音频")]
    private AudioSource audioSource;
    
    // --- 私有变量 ---
    private Rigidbody2D rb; 
    private Vector2 screenBounds;
    private float playerWidth;
    private float playerHeight;
    
    //<summary>
    //初始化组件引用和屏幕边界
    //</summary>
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.gravityScale = 0; // 无重力
        rb.drag = 10; // 线性阻尼，用于平滑停止
        audioSource = GetComponent<AudioSource>();
        
        // 在游戏开始时计算屏幕边界和玩家尺寸
        screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
        // 获取Sprite Renderer的边界，这样计算更精确
        playerWidth = transform.GetComponent<SpriteRenderer>().bounds.size.x / 2;
        playerHeight = transform.GetComponent<SpriteRenderer>().bounds.size.y / 2;
    }
    /// <summary>
    /// 调用方法，限制玩家活动范围
    /// </summary>
    void LateUpdate()
    {
        KeepPlayerInBounds();
    }
    
    /// <summary>
    /// 用于将玩家限制在屏幕范围内
    /// </summary>
    private void KeepPlayerInBounds()
    {
        // 获取玩家当前的位置
        Vector3 viewPos = transform.position;
        // 使用Mathf.Clamp将X和Y坐标限制在屏幕边界内
        // 需要从screenBounds中减去玩家自身宽度/高度的一半，以确保是玩家的边缘碰到屏幕边缘，而不是中心点
        viewPos.x = Mathf.Clamp(viewPos.x, -screenBounds.x + playerWidth, screenBounds.x - playerWidth);
        viewPos.y = Mathf.Clamp(viewPos.y, -screenBounds.y + playerHeight, screenBounds.y - playerHeight);
        // 将被“钳制”后的坐标应用回玩家
        transform.position = viewPos;
    }

    /// <summary>
    /// 获取键盘输出来控制玩家移动
    /// </summary>
    void FixedUpdate()
    {
        // --- 移动部分 ---
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector2 movement = new Vector2(h, v);
        rb.velocity = movement * speed;
    }

    /// <summary>
    /// 获取鼠标位置，改变玩家战机朝向
    /// </summary>
    void Update()
    {
        // --- 鼠标朝向部分 ---
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 10f; // 距离摄像机的深度

        Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(mousePos);
        Vector2 direction = mouseWorldPos - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

        // 校准角度
        transform.rotation = Quaternion.Euler(0, 0, angle - 90f);
        //判断是否点击鼠标左键
        if (Input.GetMouseButtonDown(0))
        {
            Shoot();
        }
    }

    /// <summary>
    /// 负责玩家射击逻辑
    /// </summary>
    void Shoot()
    {
        Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        //播放音效
        audioSource.Play();
    }
    
    /// <summary>
    /// 负责玩家死亡逻辑
    /// </summary>
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Enemy")) // 确保敌人预制体设置了"Enemy"标签
        {
            GameManager.Instance.GameOver();
            gameObject.SetActive(false);
        }
    }
}